﻿using System.Collections.Generic;
using HCQ2WebAPI_Logic.BaseAPIController;
using HCQ2_Model.ViewModel.WebAPI;
using System.Web.Http;
using HCQ2_Common.Constant;
using HCQ2WebAPI_Logic.Model;
using HCQ2_Model.VR.Model;
using HCQ2_Model.VR.Params;
using HCQ2_Model;

namespace HCQ2WebAPI_Logic.DocumentController
{
    /// <summary>
    ///  前端根据权限获取菜单、元素等资源
    /// </summary>
    public class LimitsApiController: BaseWeiXinApiLogic
    {
        #region ✔1.0 获取权限菜单 + async Task<WebApiResultJsonModel> GetLimitMenu(MenuParams param)
        /// <summary>
        ///  1.0 获取权限菜单
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetLimitMenu(MenuParams param)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(param.userid);
            param.user_id = user_id;
            List<MenuResult> result = operateContext.bllSession.T_PageFolder.GetLimitsMenu(param);
            if(result==null || result.Count<=0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "未对当前用户分配任何菜单！", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取权限菜单列表~", result);
        }
        #endregion

        #region ✔1.1 获取菜单元素 + async Task<WebApiResultJsonModel> GetMenuElement(MenuElementParams param)
        /// <summary>
        ///  1.1 获取菜单元素
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetMenuElement(MenuElementParams param)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            if(param.folder_id<=0 && string.IsNullOrEmpty(param.menu_code) && string.IsNullOrEmpty(param.module_code))
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "菜单id、菜单代码和模块代码不能同时为空！", null);
            //菜单代码
            if (param.folder_id <= 0 && !string.IsNullOrEmpty(param.menu_code))
            {
                var query = operateContext.bllSession.T_PageFolder.FindSingle(s => s.Bus_Code.Equals(param.menu_code));
                if(query==null)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "当前菜单代码无对应菜单，请核实！", null);
                param.folder_id = query.folder_id;
            }
            //1.0 判断请求页面是否存在
            if (param.folder_id > 0)
            {
                int pageCount = operateContext.bllSession.T_PageFolder.SelectCount(s => s.folder_id == param.folder_id);
                if (pageCount <= 0)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "请求的页面后台不存在，请核实！", null);
            }
            int user_id = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(param.userid);
            List<VRClientMenuModel> element = operateContext.bllSession.T_Permissions.GetElementsById(user_id, null, null, param.folder_id, param.module_code);
            List<MenuElementResult> result = new List<MenuElementResult>();
            element?.ForEach(s =>
            {
                result.Add(new MenuElementResult
                {
                    folder_id = s.folder_id,
                    pe_name = s.pe_name,
                    pe_code = s.pe_code,
                    pe_func = s.pe_func,
                    pe_note = s.pe_note,
                    menu_code = s.menu_code
                });
            });
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取菜单元素列表~", result);
        }
        #endregion

        #region ✔1.2 获取数据字典用户权限 + async Task<WebApiResultJsonModel> GetDictUserAccess(DictUserParam param)
        /// <summary>
        ///  1.2 获取数据字典用户权限
        /// </summary>
        /// <param name="param">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel<int?> GetDictUserAccess(DictUserParam param)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel<int?>.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int count = operateContext.bllSession.T_Permissions.GetDictUserAccess(param);
            return ApiResultJsonModel<int?>.RedirectWebApi(WebResultCode.Ok, "成功获取数据字典用户权限~", count);
        }
        #endregion
    }
}